import { getToken, login, tokenParse } from "@/api/identityServer.config";
import { useUserInfoStore } from "@/stores/UserInfo";
import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/Home.vue";
const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: "/",
			name: "home",
			component: HomeView,
		},
	],
});
router.beforeEach(async (to, from, next) => {
	// 接收ids回调
	if (to.path === "/callback") {
		useUserInfoStore().token = await getToken(to.query.code as string);
		next("/");
	}
	// 登录跳转
	if (!useUserInfoStore().token.access_token) {
		await login();
	}
	console.log(tokenParse(useUserInfoStore().token.access_token));

	document.title = (to.meta.title as string) || "fusionOnline-管理端";
	next();
});
export default router;
